#!/usr/bin/env python
import wx, thread
import wx.grid

class BoardDisplay(wx.Frame):
	def __init__(self, *args, **kwds):
		kwds["style"] = wx.DEFAULT_FRAME_STYLE
		wx.Frame.__init__(self, *args, **kwds)
		self.grid_1 = wx.grid.Grid(self, -1, size=(1, 1))

		self.slider_1 = wx.Slider(self, -1, 0, 0, 100, style=wx.SL_HORIZONTAL|wx.SL_LABELS)

		self.__set_properties()
		self.__do_layout()
		self.__paint_chessboard()
		
		self.Bind(wx.EVT_COMMAND_SCROLL_ENDSCROLL, self.updateLevel, self.slider_1)

	def __set_properties(self):
		self.SetTitle("frame_1")
		self.SetSize((270, 277))
		self.Hist_Length = 30
		self.grid_1.CreateGrid(8, 8)
		self.grid_1.SetRowLabelSize(30)
		self.grid_1.SetColLabelSize(30)
		self.grid_1.SetFont(wx.Font(22,wx.TELETYPE,wx.BOLD,wx.NORMAL,1))		

		#set row labels to chess orientation
		for row in range(8):
			self.grid_1.SetColSize(row, 30)
			self.grid_1.SetRowLabelValue(row, str(8 - row))

	def __do_layout(self):
		sizer_1 = wx.BoxSizer(wx.VERTICAL)
		sizer_1.Add(self.grid_1, 1, wx.EXPAND, 0)
		sizer_1.Add(self.slider_1, 0, wx.EXPAND, 0)
		self.SetSizer(sizer_1)
		self.Layout()
		
	def __paint_chessboard(self):
		for x in range(8):
			for y in range(8):
				if x%2 != y%2:
					self.grid_1.SetCellBackgroundColour(x, y, wx.BLACK)
					
	def updateLevel(self, event): # wxGlade: MyFrame.<event_handler>
		self.Hist_Length = self.slider_1.GetValue()
		self.valid = False
		event.Skip()
					
	def addPiece(self, loc, color):
		x, y = getX(loc), getY(loc)
		self.grid_1.SetCellValue(x, y, "X")
		self.grid_1.SetCellTextColour(x, y, getColor(color))
		
	def clear(self):
		self.valid = True
		for x in range(8):
			for y in range(8):
				self.grid_1.SetCellValue(x, y, "")

class chess_window(object):
	def __init__(self):
		self.app = wx.PySimpleApp(0)
		self.Board_Display = BoardDisplay(None, -1, "")
		self.valid = True

	def start(self):
		wx.InitAllImageHandlers()
		self.app.SetTopWindow(self.Board_Display)
		self.Board_Display.Show()
		self.app.MainLoop()
		
	def update(self):
		wx.InitAllImageHandlers()
		self.app.SetTopWindow(self.Board_Display)
		self.Board_Display.Show()
		self.app.MainLoop()

	def addPiece(self, loc, color):
		if self.Board_Display != "":
			self.Board_Display.addPiece(loc, color)
			
	def clear(self):
		self.Board_Display.clear()
		
	def valid(self):
		return self.Board_Display.valid
		
	def Hist_Length(self):
		return self.Board_Display.Hist_Length
		
def getY(location):
	return ord(location[0].upper()) - 65
	
def getX(location):
	return 8 - int(location[1])
	
def getColor(strColor):
	if strColor.upper() == "BLACK" or strColor.upper() == 'B':
		return wx.Color(0xCC, 0x90, 0x50)
	elif strColor.upper() == "WHITE" or strColor.upper() == 'W':
		return wx.Color(0xCC, 0xCC, 0xCC)
